package com.qinggeng.project.api.utils;

import com.qinggeng.common.enums.CommandTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.IdCodeUtils;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.common.utils.spring.SpringUtils;
import com.qinggeng.framework.redis.RedisCache;
import com.qinggeng.project.system.service.ISysConfigService;

import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 吴鹏飞
 * @date 2020/11/6
 * 口令生成工具类
 */

public class CommandUtils {


    private static String PREFIX = "*品味生活#";
    //免费领
    private static final String SUFFIXFORFREE = "【复制粘贴到榀巍盛活aPP打凯,机🉑️槌一捶免费翎】";
    //好友助力
    private static final String SUFFIXFORASSIST = "【复制粘贴到榀巍盛活aPP打凯,棒好友助历亿🦐吧】";
    //拉新
    private static final String INVIT = "【复制粘贴到榀巍盛活aPP拉新吧】";

    private static final String COMMAND = "command";

    public static void main(String[] args) {
        String s = parseCommand("*果舒达#$ba891661_20_783721$【复制粘贴到榀巍盛活aPP打凯,棒好友助历亿\\uD83E\\uDD90吧】");
        System.out.println(s);

    }
    //加密
    public static String getCommand(String commandType,String data,RedisCache redisCache,String adCode){
        ISysConfigService sysConfigService = SpringUtils.getBean(ISysConfigService.class);
        String prefix = (String) sysConfigService.selectConfigByKey("sys.command.prefix");
        if (!StringUtils.isEmpty(prefix)){
            PREFIX = prefix;
        }
        String command = "";
        //加密值
        String key = UUID.randomUUID().toString().substring(0, 8);
        if (commandType.equals(CommandTypeEnum.捶一捶.getValue())){
            redisCache.setCacheObject(key+"_"+CommandTypeEnum.捶一捶.getValue()+"_"+adCode,data,12, TimeUnit.HOURS);
            System.out.println(key);
            //拼接口令
            command = PREFIX+"$"+key+"_"+CommandTypeEnum.捶一捶.getValue()+"_"+adCode+"$"+SUFFIXFORFREE;
            System.out.println(command);
        }else if (commandType.equals(CommandTypeEnum.好友助力.getValue())){
            redisCache.setCacheObject(key+"_"+CommandTypeEnum.好友助力.getValue()+"_"+adCode,data,12, TimeUnit.HOURS);
            System.out.println(key);
            //拼接口令
            command = PREFIX+"$"+key+"_"+CommandTypeEnum.好友助力.getValue()+"_"+adCode+"$"+SUFFIXFORASSIST;
            System.out.println(command);
        }
        if (StringUtils.isEmpty(command)){
            throw new CustomException("加密失败,command="+command);
        }

        return command;
    }

    public static String getCommand(String data){
        String command = "";
        //加密值
        String key = UUID.randomUUID().toString().substring(0, 8);

        command = PREFIX+"$"+COMMAND+"_:"+key+"_"+CommandTypeEnum.捶一捶.getValue()+"$"+SUFFIXFORFREE;

        return command;
    }

    //拉新加密
    public static String getPullNewCommand(Long id){
        String command = "";
        //加密值
        String key = IdCodeUtils.toSerialCode(id);
        command = PREFIX+"$"+key+"_"+CommandTypeEnum.PULL_NEW.getValue()+"$"+INVIT;
        if (StringUtils.isEmpty(command)){
            throw new CustomException("加密失败,command="+command);
        }
        return command;
    }

    //解密
    public static String parseCommand(String command){
        String pattern = "([\\p{Sc}])\\w{8,24}([\\p{Sc}}])";
        Pattern t = Pattern.compile(pattern);
        Matcher m = t.matcher(command);
        String s ="";
        if (m.find()){
            s = m.group().replaceAll("\\$", "");
        }
        return s;
    }

}
